<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Item_sim_model extends CI_Model
{
//get all sim follow pagination
    public function getAll($numPost = '', $start = '')
    {
        $return = array();
        $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
         $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    $this->db->where(array(
	    		'I.delete_flag'    =>  0,
	    ));
        $this->db->order_by('price', 'desc'); 
        if($numPost != '' || $start != '')
            $this->db->limit($numPost, $start);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    public function getNumAll($status = 0)
    {
    	$return = array();
        $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
        $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    if($status != 1) {
	    	$this->db->where(array(
	    			'I.delete_flag'    =>  0,
	    	));
	    } else {
		    $this->db->where(array(
		    		'I.delete_flag'    =>  0,
		    		'I.discount'	   => $status,
		    ));
	    }
	    
    	return $this->db->count_all_results();
    }
//get all sim by price status : 1->giam gia, 2->tang gia, 3->Sim mới nhất
    public function getAllByPrice($priceStatus=1, $numPost = '', $start = '')
    {
    	$return = array();
    	$this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
    	$this->db->from('item_sim I');
    	$this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
    	if($priceStatus !=3 ) {
	    	$this->db->where(array(
	    			'I.delete_flag'    =>  0,
	    			'I.discount'	   => $priceStatus,
	    	));
    	} 
    	$this->db->order_by('price', 'desc');
    	if($numPost != '' || $start != '')
    		$this->db->limit($numPost, $start);
    	else 
    		$this->db->limit(20, 0);
    	
    	if($priceStatus == 3) {
    		$this->db->order_by("modified", "desc");
    		
    	}
    	$rs = $this->db->get();
    	$return = $rs->result_array();
    	$rs->free_result();
    	return $return;
    	
    	
    } 
	public function getListSimByNetworkId($id, $numPost = '', $start = '',$order='desc')
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
	    $return = array();
	    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
	    $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    $this->db->where(array(
	    		'I.delete_flag'    =>  0,
	    		'I.list_network_id' => $id
	    ));
	    
	    $this->db->order_by('price', 'asc');
	    if($numPost != '' || $start != '')
	    	$this->db->limit($numPost, $start);
	    $rs = $this->db->get();
	    if ($rs->num_rows() > 0) {
	            $return = $rs->result_array();
	            $rs->free_result();
	            return $return;
        } else {
            $rs->free_result();
            
            return false;
        }
    }
    
    public function getCountListSimByNetworkId($id)
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
	    $return = array();
	    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, L.name");
	    $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    $this->db->where(array(
	    		'I.delete_flag'    =>  0,
	    		'I.list_network_id' => $id
	    ));
	    
	    return $this->db->count_all_results();
    
    }
    
    //get group
    public function getListSimByGroupId($id, $numPost = '', $start = '',$order='desc')
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
    $return = array();
    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
    $this->db->from('item_sim I');
    $this->db->join('list_group L', 'I.list_group_id = L.id', 'inner');
    $this->db->where(array(
    		'I.delete_flag'    =>  0,
    		'I.list_group_id' => $id
    ));
     
    $this->db->order_by('price', 'asc');
    if($numPost != '' || $start != '')
    	$this->db->limit($numPost, $start);
    $rs = $this->db->get();
    if ($rs->num_rows() > 0) {
    	$return = $rs->result_array();
    	$rs->free_result();
    	return $return;
    } else {
    	$rs->free_result();
    
    	return false;
    }
    }
    
    public function getCountListSimByGroupId($id)
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
    $return = array();
    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, L.name");
    $this->db->from('item_sim I');
    $this->db->join('list_group L', 'I.list_group_id = L.id', 'inner');
    $this->db->where(array(
    		'I.delete_flag'    =>  0,
    		'I.list_group_id' => $id
    ));
     
    return $this->db->count_all_results();
    
    }
    
    public function getListSimByNetworkIdAndGroupId($id, $groupId, $numPost = '', $start = '',$order='desc')
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
	    $return = array();
	    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price_update, L.name");
	    $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    $this->db->where(array(
	    		'I.delete_flag'    =>  0,
	    		'I.list_network_id' => $id,
	    		'I.list_group_id' => $groupId,
	    ));
	     
	    $this->db->order_by('price', 'asc');
	    if($numPost != '' || $start != '')
	    	$this->db->limit($numPost, $start);
	    $rs = $this->db->get();
	    if ($rs->num_rows() > 0) {
	    	$return = $rs->result_array();
	    	$rs->free_result();
	    	return $return;
	    } else {
	    	$rs->free_result();
	    	return false;
	    }
    }

    public function getCountListSimByNetworkIdAndGroupId($id, $groupId) {
    // 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
	    $return = array();
	    $this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, L.name");
	    $this->db->from('item_sim I');
	    $this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
	    $this->db->where(array(
	    		'I.delete_flag'    =>  0,
	    		'I.list_network_id' => $id,
	    		'I.list_group_id' => $groupId,
	    ));
	    
	    return $this->db->count_all_results();
    }
    public function searchArray($textSearch, $networkId, $groupId, $priceStart, $priceEnd, $dauso='%', $dau=0,  $order='desc',$numPost = '', $start = '')
    {// 	id 	phone 	delete_flag 	list_city_id 	list_group_id 	list_network_id 	price
	    $return = array();
	    $sql = 'SELECT I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, I.price, N.name '
	    	  .'FROM item_sim I '
	    	  .'LEFT JOIN list_network N ON I.list_network_id = N.id '
	    	  .'LEFT JOIN list_group G ON I.list_group_id = G.id '
	    	  .'WHERE I.delete_flag=0 '
	    	  .'AND I.phone LIKE "'.$textSearch.'" '
	    	  .'AND I.price >= "'.$priceStart.'" '
	    	  .'AND I.price <= "'.$priceEnd.'" '
	    	  .'AND I.phone LIKE "%'.$dauso.'%" ';
		
	    if($networkId != 0) {
	    	$sql = $sql . 'AND I.list_network_id = '.$networkId.' ';
	    }
	    if($groupId != 0) {
	    	$sql = $sql . 'AND I.list_group_id = '.$groupId . ' ';
	    } 
		if($dau !=0) {
			$sql = $sql .'AND LENGTH(I.phone) = '.$dau;
		}
		$sql = $sql.'ORDER BY I.price '.$order .' ';
	    $query = $this->db->query($sql);
	    if ($query->num_rows() > 0) {
	    	$return = $query->result_array();
	    	$query->free_result();
	    	return $return;
	    } else {
	    	$query->free_result();
	    	return false;
	    }
    }
    public function getById($id)
    {
    	$return = array();
    	$this->db->select("I.id, I.phone, I.list_city_id, I.list_group_id, I.list_network_id, I.price, L.name");
    	$this->db->from('item_sim I');
    	$this->db->join('list_network L', 'I.list_network_id = L.id', 'inner');
    	$this->db->where(array(
    			'I.delete_flag'    =>  0,
    			'I.id'=>$id,
    	));

    	$rs = $this->db->get();
    	$return = $rs->result_array();
    	$rs->free_result();
    	return $return[0];
    }
    //thong ke sim
    
} 


?>
